<?php

namespace app\index\controller;

use think\Controller;
use Db;

class Movie extends Base
//class Movie extends Controller
{


    //电影分类页
    public function index()
    {

        $types = Db::name('movie_type')->field('id,type_name,image')->where(['is_type' => 1])->order('rank_type desc,id desc')->select();

        $this->assign('types', $types);

        $this->assign('img_path', config('api.oss_img_path'));
        return $this->fetch();
    }

    //根据类型获取电影列表
    public function getList()
    {

        $type = input('type_id');

        if (!$type) {
            $this->error('未知类型');
        }

        $list = Db::name('lesson')->field('id,lesson_name')->where(['type' => $type, 'status' => 1])->order('rank desc,id desc')->select();

        $this->success('success', '', $list);
    }

    //电影详情页
    public function details()
    {

        $id = input('id');

        if (!$id) {
            $this->error('未知电影');
        }

        $movie = Db::name('movie')->where(['status' => 1, 'id' => $id])->find();

        
        if (!$movie) {
            $this->error('未知电影');

        }

        $img_path = config('api.oss_img_path');
        $this->assign('img_path', $img_path);

        $tuijian = rand_movie();
        $this->assign('tuijian',$tuijian);
//        $invite_code = $this->userInfo['invite_code'];
//
//        if(empty($invite_code)){
//            //生成邀请码
//            $invite_code = strtoupper(sprintf('%x',crc32(microtime().$this->uid)));
//
//            $upd = Db::name('user')->where('id',$this->uid)->update(['invite_code'=>$invite_code]);
//            if(!$upd){
//                $this->error('生成邀请码失败,请重新稍后重试！');
//            }
//        }
        //电影分享链接
//        $share_url=request()->domain().'/index/lesson/details/id/'.$id.'/?code='.$invite_code;

//        $this->assign('share_url',$share_url);

        $movie['types'] = Db::name('movie_type')->field('type_name')->whereIn('id', $movie['types'])->select();
        $this->assign('movie', $movie);

        return $this->fetch();
    }

    public function play()
    {
        $id = input('id');
        $section_id = input('sid');
        if (!$id && !$section_id) {
            $this->error('未知电影');
        }

        if ($id) {
            $movie = Db::name('movie')->where(['status' => 1, 'id' => $id])->find();

        } else {

            $section = Db::name('movie_section')->where('id', $section_id)->find();

            if (!$section) {
                $this->error('电影不存在');

            }

            $movie = Db::name('movie')->where(['status' => 1, 'id' => $section['movie_id']])->find();
        }

        if (!$movie) {
            $this->error('未知电影');
        }


        if (empty($section)) {

            $section = Db::name('movie_section')->where('movie_id', $movie['id'])->order('rank asc,id asc')->find();
        }

        $this->assign('section', $section);

        $sections = Db::name('movie_section')->where('movie_id', $movie['id'])->where('status',1)->order('rank asc,id asc')->select();
        $this->assign('sections', $sections);

        $img_path = config('api.oss_img_path');
        $this->assign('img_path', $img_path);

        $movie['types'] = Db::name('movie_type')->field('type_name')->whereIn('id', $movie['types'])->select();
        $this->assign('movie', $movie);

        $tuijian = rand_movie();
        $this->assign('tuijian',$tuijian);

        //播放记录
        $play_time = Db::name('look_record')->where(['uid'=>$this->uid,'section_id'=>$section['id']])->value('play_time');
        $this->assign('play_time',$play_time);

        //是否免费观看
        $try_see = Db::name('config')->where('key','try_see')->value('value');

        $user = $this->userInfo;

        $time = time();

        if($user['level_validity']>$time || $user['sales_validity']>$time){
            $try_see = 0;
        }

        $ext =  substr($section['url'], strrpos($section['url'], '.')+1);
        $this->assign('ext',$ext);
        $this->assign('try_see',$try_see);

        if(input('pp')){
                    return $this->fetch('play2');

        }
        return $this->fetch();
    }

    public function dplayer()
    {
        $id = input('id');
        $section_id = input('sid');
        if (!$id && !$section_id) {
            $this->error('未知电影');
        }

        if ($id) {

            $movie = Db::name('movie')->where(['status' => 1, 'id' => $id])->find();

        } else {

            $section = Db::name('movie_section')->where('id', $section_id)->find();

            if (!$section) {
                $this->error('电影不存在');

            }

            $movie = Db::name('movie')->where(['status' => 1, 'id' => $section['movie_id']])->find();
        }

        if (!$movie) {
            $this->error('未知电影');
        }


        if (empty($section)) {

            $section = Db::name('movie_section')->where('movie_id', $movie['id'])->order('rank asc,id asc')->find();
        }

        $this->assign('section', $section);


        $img_path = config('api.oss_img_path');
        $this->assign('img_path', $img_path);

//        $movie['types'] = Db::name('movie_type')->field('type_name')->whereIn('id', $movie['types'])->select();
        $this->assign('movie', $movie);



        //播放记录
        $play_time = Db::name('look_record')->where(['uid'=>$this->uid,'section_id'=>$section['id']])->value('play_time');
        $this->assign('play_time',$play_time);

        //是否免费观看
        $try_see = Db::name('config')->where('key','try_see')->value('value');

        $user = $this->userInfo;

        $time = time();

        if($user['level_validity']>$time || $user['sales_validity']>$time){
            $try_see = 0;
        }

        $this->assign('try_see',$try_see);

        return $this->fetch();
    }

    public function ckplayer()
    {
        $id = input('id');
        $section_id = input('sid');
        if (!$id && !$section_id) {
            $this->error('未知电影');
        }

        if ($id) {

            $movie = Db::name('movie')->where(['status' => 1, 'id' => $id])->find();

        } else {

            $section = Db::name('movie_section')->where('id', $section_id)->find();

            if (!$section) {
                $this->error('电影不存在');

            }

            $movie = Db::name('movie')->where(['status' => 1, 'id' => $section['movie_id']])->find();
        }

        if (!$movie) {
            $this->error('未知电影');
        }


        if (empty($section)) {

            $section = Db::name('movie_section')->where('movie_id', $movie['id'])->order('rank asc,id asc')->find();
        }

        $this->assign('section', $section);


        $img_path = config('api.oss_img_path');
        $this->assign('img_path', $img_path);

//        $movie['types'] = Db::name('movie_type')->field('type_name')->whereIn('id', $movie['types'])->select();
        $this->assign('movie', $movie);



        //播放记录
        $play_time = Db::name('look_record')->where(['uid'=>$this->uid,'section_id'=>$section['id']])->value('play_time');
        $this->assign('play_time',$play_time);

        //是否免费观看
        $try_see = Db::name('config')->where('key','try_see')->value('value');

        $user = $this->userInfo;

        $time = time();

        if($user['level_validity']>$time || $user['sales_validity']>$time){
            $try_see = 0;
        }

        $this->assign('try_see',$try_see);

        return $this->fetch();
    }

    public function search()
    {

        $keyword = input('keyword');
        $this->assign('keyword', $keyword);

        if ($keyword) {

            $search = ['query'=>[]];
            $search['query']['keyword'] = $keyword;;

            $list = Db::name('movie')->where("status=1 and (movie_name like '%$keyword%' or stars like '%$keyword%' or director like '%$keyword%')")
                ->paginate(10, false,$search)->each(function ($item, $key) {
                    $item['types'] = Db::name('movie_type')->field('type_name')->whereIn('id', $item['types'])->select();
                    return $item;
                });
        }else{
            $list = [];
        }

        $img_path = config('api.oss_img_path');
        $this->assign('img_path', $img_path);
        $this->assign('list', $list);

        $tuijian = rand_movie();
        $this->assign('tuijian',$tuijian);
        return $this->fetch();
    }


    public function randMovie(){
        $tuijian = rand_movie();

        return ['code'=>200,'data'=>$tuijian];
    }

    //分类列表
    public function lists()
    {
        $type = input('type');
        $search = ['query'=>[]];
        $search['query']['type'] = $type;;
        $list = Db::name('movie')->field('id,movie_name,image')->where("status=1 and find_in_set('$type',types)")->paginate(15, false,$search);

        $img_path = config('api.oss_img_path');
        $this->assign('list',$list);
        $this->assign('img_path',$img_path);
        return $this->fetch();
    }

    //播放记录
    public function playRecord(){
        $data = input();

        $data['ip'] = ip2long(request()->ip());
        $data['create_time'] = time();
        $data['uid'] = $this->uid;

        $record_id = Db::name('look_record')->where(['uid'=>$data['uid'],'movie_id'=>$data['movie_id']])->value('id');

        if($record_id){
            //更新播放记录
            Db::name('look_record')->where('id',$record_id)->update($data);
        }else{
            Db::name('look_record')->insert($data);
        }

        //删除旧记录
        $old = Db::name('look_record')->field('id')->where('uid',$data['uid'])->order('create_time desc')->limit(50,1)->select();

        if(count($old)>0){

            Db::name('look_record')->where('uid',$data['uid'])->where('id','<=',$old[0]['id'])->delete();
        }

        return 'success';

    }
}
